package org.classg.edu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.classg.edu.entity.Teacher;
import org.classg.edu.service.CourseService;
import org.classg.edu.service.TeacherService;
import org.classg.edu.entity.Course;
import org.classg.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(description = "前台首页显示")
@RequestMapping("/eduservice/index")
@RestController
@CrossOrigin
public class IndexController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private TeacherService teacherService;

    @Cacheable(key = "'queryCourseAndTeacher'", value = "coursesAndTeachers")
    @ApiOperation("查询前8条热门课程，查询前4条名师")
    @GetMapping("/queryCourseAndTeacher")
    public R queryCourseAndTeacher() {
        // 查询前8条热门课程
        List<Course> courses = courseService.list(new LambdaQueryWrapper<Course>().orderByDesc(Course::getId).last("limit 8"));
        // 查询前4条名师
        List<Teacher> teachers = teacherService.list(new LambdaQueryWrapper<Teacher>().orderByDesc(Teacher::getId).last("limit 4"));
        return R.ok().data("courses", courses).data("teachers", teachers);
    }
}
